 < !DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>用户管理</title>
<link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
<script src="../../layui/layui.js"></script>
<script src="../../js/jquery-1.8.3.js"></script>
<script src="../../js/util.js"></script>
<script>
    
    
            function addUser() {
    		var duty = $("#duty").val();
    		$.ajax({
    			type : 'post',
    			async : false,
    			url : '/util/findUser',
    			success : function(json) {
    				user = json;
    				if (json.duty != 3) {
    					location.href = "operatorAdd.html";
    				} else {
    					layer.open({
    						content : '权限不足',
    						scrollbar : false
    					});
    				}
    			},
    			error : function() {
    				layer.msg('系统异常，请联系管理员处理!');
    			}
    		});    		
        } 

        layui.use(['table', 'form', 'jquery','element'], function () {
            var table = layui.table;
            var form = layui.form;
            var $ = layui.$;
       
            var tabledata = {
                elem: "#table",//表格id
                id: "modelReload", //id
                url: '/user/find',
                method: 'post',
                request: {
                    pageName: 'page',
                    limitName: 'limit'
                },
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    msgName: 'msg',
                    countName: 'count',
                    dataName: 'data'
                },
                page: true,
                count: "count",
                limit: 10,
                limits: [10, 20, 30, 40, 50],
                layout: ['prev', 'page', 'next'],
                prev: "上一页",
                next: "下一页",
                cols: [[ //表头
                      {field: 'province', title: '省份/地市/区县'}
                    , {field: 'userName', title: '用户姓名'}
                    , {field: 'msisdn', title: '手机号码'}
                    , {field: 'dutyName', title: '级别'}
                    , {field: 'smsRuleName', title: '角色'}
                    , {field: 'createTime', title: '创建时间'}
                    , {field: 'checkName', title: '是否审核人'}
                    , {field: 'operation', title: '操作', toolbar: '#barDemo'}
                ]],
                                done: function (res) {
                    // console.log(JSON.stringify(res));
                }
            };
		table.render(tabledata);

		function selectduty(duty) { //点击按钮触发权限控制操作
			var duty = $("#duty").val();
			if(duty==0){
			cityId = "25";
			}
			var isCheck = $("#isCheck").val();
			$.ajax({
				type : 'post',
				async : false,
				url : '/util/findUser',
				success : function(json) {
					user = json;
					if (json.duty != 0) {
						if ((duty == json.duty) || (duty == (json.duty - 1))) //duty等于权限或权限减一时，操作可以点击
						{

							table.on('tool(demo)', function(obj) {
								var data = obj.data;
								if (obj.event === 'del') {
									layer.open({
										content : '权限不足',
										scrollbar : false
									});
								} else if (obj.event === 'edit') {
									layer.open({
										content : '权限不足',
										scrollbar : false
									});
								}
							});
						}
						 else {
						table.on('tool(demo)', function(obj) {
							var data = obj.data;
							if (obj.event === 'del') {
								layer.confirm('您确定要删除吗', {
									icon : 3,
									title : '提示'
								}, function(index) {
									var url = "/user/remove";
									var param = {
										userId : data.userId
									};
									$.ajax({
										type : 'post',
										data : param,
										url : url,
										beforeSend : function() {},
										success : function(data) {
											layui.use('layer', function() {
												if (data == 1) {
													layer.confirm('删除成功', {
														btn : [ '确认' ] //按钮
													}, function() {
														location.href = "operatorManage.html";
													});
												} else {
													layer.confirm('删除失败，无权限', {
														btn : [ '确认' ] //按钮
													}, function() {
														location.href = "operatorManage.html";
													});
												}
											});
										},
										complete : function() {},
										error : function(data) {
											layer.msg('系统异常，请联系管理员处理!');
										}
									});
								});

							} else if (obj.event === 'edit') {
								location.href = "operatorModify.html?userId=" + data.userId;
							}
						});
					}
					} else {
						table.on('tool(demo)', function(obj) {
							var data = obj.data;
							if (obj.event === 'del') {
								layer.confirm('您确定要删除吗', {
									icon : 3,
									title : '提示'
								}, function(index) {
									var url = "/user/remove";
									var param = {
										userId : data.userId
									};
									$.ajax({
										type : 'post',
										data : param,
										url : url,
										beforeSend : function() {},
										success : function(data) {
											layui.use('layer', function() {
												if (data == 1) {
													layer.confirm('删除成功', {
														btn : [ '确认' ] //按钮
													}, function() {
														location.href = "operatorManage.html";
													});
												} else {
													layer.confirm('删除失败，无权限', {
														btn : [ '确认' ] //按钮
													}, function() {
														location.href = "operatorManage.html";
													});
												}
											});
										},
										complete : function() {},
										error : function(data) {
											layer.msg('系统异常，请联系管理员处理!');
										}
									});
								});

							} else if (obj.event === 'edit') {
								location.href = "operatorModify.html?userId=" + data.userId;
							}
						});
					}

				},
				error : function() {
					layer.msg('系统异常，请联系管理员处理!');
				}
			});
		}
/* 
		             //监听操作按钮隐藏
		            form.on('select(duty)', function (duty) {
		               var duty = $("#duty").val();           
		               if((duty == 0)||(duty =="")){
		               $("#cityId").attr("disabled", true);
		               $("#countyId").attr("disabled", true);
		               }                                                                    
		            });  */


		//监听区县
		form.on('select(cityId)', function (data) {
                var data1 = selectCountyId(data.value);
                appendCounty(data1);
            });
            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接区县
            function appendCounty(data){
                 var options = '<option value="">全部</option>';
                if (data.length != null) {
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
		}
		
		//身份识别
		$(document).ready(function () {
                $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                        user=json;
    					if (json.duty == 0) { //省管理员
    
    					} else if (json.duty == 1) { //地市管理员
    					    $('#quanbu').remove();
    						$("#cityId").val(json.cityId);
    						$("#cityId").attr("disabled", true);
    						var data1 = selectCountyId(json.cityId);
    						appendCounty(data1);
    						form.render();   
    					} else if (json.duty == 2){ //区县管理员
    					    $('#quanbu').remove();
    					    $('#sheng').remove();
    					    $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data1 = selectCountyId(json.cityId);
                            appendCounty(data1);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
    					}else if (json.duty == 3){ //项目管理员
    					    $('#quanbu').remove();
    					    $('#sheng').remove();
    					    $('#dishi').remove();    					   
                            var data1 = selectCountyId(json.cityId);
                            appendCounty(data1);
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
    					}
    					
    				}, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
            }); 
                                        
             $('#find').on('click', function () {               
                table.reload('modelReload', {
                    method: 'post',
                    page: {
                        curr: 1
                    },
                    where: {
                        name: $("#name").val(),
                        msisdn: $("#msisdn").val(),
                        duty: $("#duty").val(),                  
                        cityId: $("#cityId").val(),
                        countyId: $("#countyId").val(),
                        isCheck: $("input[name='isCheck']:checked").val(),
                        
                    }
                });
                  selectduty();
            }); 
        });

    </script>
<script type="text/html" id="barDemo">
        <a href="#" lay-event="edit">修改</a>&nbsp;&nbsp;&nbsp;
        <a href="#" lay-event="del">删除</a>
    </script>
</head>
<body>
	<div class="layui-content-body">
		<div class="scroll-box">
			<div class="breadcrumb-box">
				<span class="layui-breadcrumb" lay-separator="|"> <a
					href="javascript:returnIndex2()">首页&nbsp;</a> <a href="">
						&nbsp;系统管理&nbsp; </a> <a href=""> &nbsp;用户管理&nbsp; </a>
				</span>
			</div>
			<div class="content-box">
				<div class="form-select-box">
					<div class="layui-form layui-form-pane" action="">
						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label">级别</label>
								<div class="layui-input-block" style="width:150px">
									<select name="duty" lay-filter="duty" id="duty">
										<option id="quanbu" value="">全部</option>
										<option id="sheng" value="0">省公司管理员</option>
										<option id="dishi" value="1">地市管理员</option>
										<option id="quxian" value="2">区县管理员</option>
										<option id="xiangmu" value="3">项目管理员</option>
									</select>
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">地市</label>
								<div class="layui-input-block" style="width:150px">
									<select name="cityId" lay-filter="cityId" id="cityId">
										<option value="">全部</option>
										<option value="11">苏州</option>
										<option value="12">淮安</option>
										<option value="13">宿迁</option>
										<option value="14">南京</option>
										<option value="15">连云港</option>
										<option value="16">徐州</option>
										<option value="17">常州</option>
										<option value="18">镇江</option>
										<option value="19">无锡</option>
										<option value="20">南通</option>
										<option value="21">泰州</option>
										<option value="22">盐城</option>
										<option value="23">扬州</option>
									</select>
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">区县</label>
								<div class="layui-input-block" style="width:150px">
									<select name="countyId" lay-filter="countyId" id="countyId">
										<option value="">全部</option>
									</select>
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">手机号码</label>
								<div class="layui-input-block">
									<input type="text" name="province" lay-verify="required"
										placeholder="请输入" autocomplete="off" class="layui-input"
										id="msisdn">
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">用户姓名</label>
								<div class="layui-input-block">
									<input type="text" name="province" lay-verify="required"
										placeholder="请输入" autocomplete="off" class="layui-input"
										id="name">
								</div>
							</div>
							<div class="layui-form-item">
								<div class="layui-block" pane="">
									<label class="layui-form-label" style="width:120px">是否审核人</label>
									<div class="layui-input-block">
										<input type="radio" name="isCheck" value="" title="全部" checked>
										<input type="radio" name="isCheck" value="0" title="是">
										<input type="radio" name="isCheck" value="1" title="否">
									</div>
								</div>
							</div>
							<div class="layui-inline" pane="">
								<button class="layui-btn layui-btn-normal" id="find">查询</button>
								<button class="layui-btn layui-btn-normal" type="button"
									onclick="addUser()">添加</button>
							</div>
						</div>
					</div>
				</div>
				<div class="table-box">
					<table class="layui-table" id="table" lay-filter="demo">
					</table>
				</div>
			</div>
		</div>
	</div>
</body>
</html>